<?php
class SaleOrderPublicStatus extends Status {
	
	 public $SALE_FLOW_SALE_ORDER 			= 1; 	//销售单
	 public $SALE_FLOW_SALE_PRE_DELIVERY 	= 2; 	//配货单
	 public $SALE_FLOW_SALE_DELIVERY 		= 3; 	//发货单 
	 
	/**
	 * 装柜结束后更新订单信息
	 *
	 * @param unknown_type $params
	 */
	protected function getState($params){ 
		 $id	=	$params['id']; 
		 if ($id>0){
		 	 switch ($params['flow_type']){
			 	case 'sale_order'://销售
			 		$this->setStateWithSale($id);
			 		break;
			 	case 'PreDelivery':	//配货 
			 		$this->setStateWithPreDelivery($id);
			 		break;
			 	case 'Delivery':	//发货
			 		$this->setStateWithDelivery($id);
			 		break;
		 	}
		 }  
	}
	
	/**
	 * 销售单状态
	 *
	 * @param unknown_type $id
	 */
	public function setStateWithSale($id){
		//判断销售是否有后续的流程(例如发货) 
		if (C('sale.relation_sale_follow_up')==1){//销售是否有后续流程 
			$this->setSaleOrderState($id,$this->SALE_FLOW_SALE_ORDER);  
		}else{ 
			$this->setSaleOrderState($id,$this->SALE_FLOW_SALE_DELIVERY); 
		} 
	}
	
	/**
	 * 配货单
	 *
	 * @param unknown_type $id 发货单ID
	 */
	public function setStateWithPreDelivery($id){ 
		//判断是否已全部完成 
		if ($id>0){
			//获取明细的销售单信息
			$model			=	M('pre_delivery');
			$main			=	$model->find($id);
			if (is_array($main)){
				$sale_order_id	=	$main['sale_order_id'];
				//修改销售单状态
				$this->setSaleOrderState($sale_order_id,$this->SALE_FLOW_SALE_PRE_DELIVERY); 
			}else { 
				//删除配货单
				$this->setStateWithPreDeliveryDelete($id);
			}
		}		
	}
	
	/**
	 * 删除配货单状态
	 *
	 * @param unknown_type $id 发货单ID
	 */
	public function setStateWithPreDeliveryDelete($id){ 
		//判断是否已全部完成
		if ($id>0){
			//获取明细的销售单信息
			$model			=	M('pre_delivery_del');
			$main			=	$model->find($id); 
			$sale_order_id	=	$main['sale_order_id'];
			//修改销售单状态
			$this->setSaleOrderState($sale_order_id,$this->SALE_FLOW_SALE_ORDER);   
		}		
	}
	
	/**
	 * 发货单
	 *
	 * @param unknown_type $id 发货单ID
	 */
	public function setStateWithDelivery($id){  
		//判断是否全部都发货了
		$main			=	M('delivery');
		$mainInfo		=	$main->find($id);  
		if (is_array($mainInfo)){
			//获取明细的销售单信息
			$model			=	M('delivery_detail');
			$detailInfo		=	$model->where('delivery_id='.$id)->find();
			$sale_order_id	=	$detailInfo['sale_order_id'];
			//判断是否已全部完成
			if ($mainInfo['sale_finish']==1){ 
				//修改销售单状态
				$this->setSaleOrderState($sale_order_id,$this->SALE_FLOW_SALE_DELIVERY); 
			}else{  
				if (C('delivery.relation_predelivery')==1){//判断是否有配货 
					$this->setSaleOrderState($sale_order_id,$this->SALE_FLOW_SALE_PRE_DELIVERY);  
				}else{ 
					$this->setSaleOrderState($sale_order_id,$this->SALE_FLOW_SALE_ORDER); 
				} 
			}
		}else{  
			$this->setStateWithDeliveryDelete($id); 
		} 
	} 
	
	/**
	 * 删除发货单
	 *
	 * @param unknown_type $id
	 */
	public function setStateWithDeliveryDelete($id){
		//判断是否全部都发货了  
		$model			=	M('delivery_detail_del');
		$detailInfo		=	$model->where('delivery_id='.$id)->find();  
		$sale_order_id	=	$detailInfo['sale_order_id'];  
		if (C('sale.relation_sale_follow_up')==1){//销售是否有后续流程 
			if (C('delivery.relation_predelivery')==1){//销售是否有后续流程 
				$this->setSaleOrderState($sale_order_id,$this->SALE_FLOW_SALE_PRE_DELIVERY);   
			}else{
				$this->setSaleOrderState($sale_order_id,$this->SALE_FLOW_SALE_ORDER);  
			}
		}else{ 
			$this->setSaleOrderState($sale_order_id,$this->SALE_FLOW_SALE_ORDER); 
		} 
	}
	
	/**
	 * 修改销售单状态
	 *
	 * @param unknown_type $sale_order_id
	 * @param unknown_type $state
	 */
	public function setSaleOrderState($sale_order_id,$state){
		if ($sale_order_id>0){
			$model = M('SaleOrder'); 
			$data = array('sale_order_state'=>$state,); 
			$model->where('id='.$sale_order_id)->setField($data);   
//			echo $model->getLastSql();
//			exit;
		} 
	}
}